package com.tangkun.environment.activity;

import android.net.http.SslError;
import android.os.Bundle;
import android.text.TextUtils;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.tangkun.environment.R;
import com.tangkun.environment.entity.shouye.NewsViewReturn;
import com.tangkun.environment.network.MySubscribe;
import com.tangkun.environment.rx.RxActivityHelper;
import com.tangkun.environment.utils.JumpHelper;
import com.tangkun.environment.utils.Utils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * @author tangkun
 * @Description:咨询详情
 */

public class WebZiXunActivity extends BaseActivity {
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.title)
    TextView titleTv;
    @BindView(R.id.webview_web)
    WebView webView;
    @BindView(R.id.tv_zixundetail_name)
    TextView tvZixundetailName;
    @BindView(R.id.tv_zixundetail_riqi)
    TextView tvZixundetailRiqi;
    @BindView(R.id.tv_zixundetail_leixing)
    TextView tvZixundetailLeixing;

    private WebSettings webSettings;
    /**
     * id
     */
    private String id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_zixun);
        ButterKnife.bind(this);
        id = getIntent().getStringExtra("id");
        titleTv.setText("详情");
        webSettings = webView.getSettings();
        webSettings.setSupportZoom(true);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setTextSize(WebSettings.TextSize.NORMAL);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setBuiltInZoomControls(false);// 页面支持缩放
        webSettings.setUseWideViewPort(false);// 设置此属性，可任意比例缩放
        webSettings.setLoadWithOverviewMode(false);// 打开页面时， 自适应屏幕
        webSettings.setDomStorageEnabled(true);// h5兼容

        webView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.NARROW_COLUMNS);//支持内容重新布局
        webView.setBackgroundColor(0);//取消背景色
        webView.getSettings().setDefaultTextEncodingName("UTF -8");//设置默认为utf-8

        // 修复链接跳转到手机自带浏览器而不是在webView上显示的问题
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            /** 跳过网站中Ssl证书错误，跳过直接登陆网站 **/
            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
            }

        });
        newsView();
    }

    /**
     * 咨询详情
     */
    private void newsView() {
        fromNetwork.newsView(id).compose(new RxActivityHelper<NewsViewReturn>().ioMain(WebZiXunActivity.this, true))
                .subscribe(new MySubscribe<NewsViewReturn>(WebZiXunActivity.this, true) {
                    @Override
                    protected void success(NewsViewReturn resultBean) {
                        if (resultBean.data != null) {
                            //标题
                            if (!TextUtils.isEmpty(resultBean.data.title)) {
                                tvZixundetailName.setText(resultBean.data.title);
                            } else {
                                tvZixundetailName.setText("");
                            }
                            //日期
                            if (!TextUtils.isEmpty(resultBean.data.createtime)) {
                                tvZixundetailRiqi.setText(Utils.getDateYearMonthDay(resultBean.data.createtime));
                            } else {
                                tvZixundetailRiqi.setText("");
                            }
                            //类型
                            if (!TextUtils.isEmpty(resultBean.data.typename)) {
                                tvZixundetailLeixing.setText(resultBean.data.typename);
                            } else {
                                tvZixundetailLeixing.setText("");
                            }
                            //加载h5源码
                            webView.loadDataWithBaseURL("", resultBean.data.content, "text/html", "UTF-8", "");
                        }
                    }

                    @Override
                    protected void onMyError(String errorMessage) {
                        showToast(errorMessage + "");
                    }
                });
    }

    @OnClick(R.id.back)
    public void onClick() {
        Utils.hideSoftKeyboard(WebZiXunActivity.this, back);
        JumpHelper.finish(WebZiXunActivity.this);
    }
}
